package main.java.common;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;

import main.java.ERMDE.controllers.IProjectController;

public class ProjectCreateView extends JPanel {
 
    private static final long serialVersionUID = 1L;

    private IProjectController projectController;

    private JDialog createDlg = new JDialog();
    private DefaultListModel<String> types = new DefaultListModel<String>();
    private JList<String> projectType = new JList<String>(types);
    private JScrollPane typeWrapper = new JScrollPane(projectType);
    private JButton btnSelect;
    private JTree tree;
    
    public ProjectCreateView() {
	createDlg.add(typeWrapper, BorderLayout.CENTER);
	this.btnSelect = new JButton("Create");
	types.addElement("ERM");
	types.addElement("FSM");
	types.addElement("DM");
	initializeSelect();
    }
 
    private void initializeSelect(){
	this.btnSelect.addMouseListener(new MouseAdapter() {
	    @Override
	    public void mouseClicked(MouseEvent e) {
		createDlg.setVisible(false);
		String name = (String) projectType.getSelectedValue();
		if (name != null && !(name.equals(""))){
		    String projectName = JOptionPane.showInputDialog(null,
				"Provide the project's name", "New Project",
				JOptionPane.QUESTION_MESSAGE);
			if (projectName != null && !(projectName.equals(""))) {
			    if(name.equals("ERM"))
				projectController.createProjectERM(projectName);
			    if(name.equals("FSM"))
				projectController.createProjectFSM(projectName);
			    if(name.equals("DM"))
				projectController.createProjectDM(projectName);
			    tree.setModel(projectController.getProjectTree());
			}
		}
	    }
	});
    }
     
    public void createDialog(JButton button){

	createDlg.setLocationRelativeTo(button);
	createDlg.setTitle("Create Project");
	createDlg.setMinimumSize(new Dimension(200,50));
	createDlg.add(btnSelect, BorderLayout.SOUTH);
	createDlg.setVisible(true);
	createDlg.pack();
    }

    public void setController(IProjectController projectController) {
	this.projectController = projectController;
    }

    public void setTree(JTree tree) {
	this.tree = tree;
    }
 
}
